package com.vdongchina.thread.base;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * 线程开始第一课
 * <p>
 * 主线程的监控
 * <p>
 * [5]Monitor Ctrl-Break
 * [4]Signal Dispatcher  分发处理发送给jvm信号的线程
 * [3]Finalizer      调用finalize方法线程
 * [2]Reference Handler 清除reference线程
 * [1]main  用户程序入口
 */
public class ThreadMainDemo {

    public static void main(String[] args) {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程1");
                try {
                    Thread.sleep(50000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setName("yiyongyao");
        thread.start();

        //获取java线程管理MXBean
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        //不需要获取同步的monitor和synchronizer信息,仅获取线程和线程堆栈的信息
        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, false);
        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println("[" + threadInfo.getThreadId() + "]" + threadInfo.getThreadName());
        }
        System.out.println(threadMXBean.getThreadCount());
    }


}
